import QtQuick
import Qt5Compat.GraphicalEffects
import QtQuick.Controls
import QtQuick.Layouts

import Fk
import Fk.Components.Common
import Fk.Components.LunarLTK
import Fk.Widgets as W

W.PageBase {
    id: root
    visible: true
    z: 99999

    property int count: 0
    property int maxCount: 4000
    property int minCount: 0
    property var counts: [350000, 250000, 200000, 160000, 130000, 80000, 60000, 52000, 12000, 8000, 4000, 0]
    property var rankAndCount: {
        "骁卒": 0,
        "校尉": 4000,
        "郎将": 8000,
        "偏将军": 12000,

        "将军": 52000,
        "上将军": 60000,
        "国护军": 80000,

        "国都护": 130000,
        "卫将军": 160000,
        "车骑将军": 200000,
        "骠骑将军": 250000,

        "大将军": 350000
    }
    property string bottomName: "官阶"

    property bool isLobby: false

    FontLoader {
        id: fontSimkai
        source: "../fonts/simkai.ttf"
    }

    FontLoader {
        id: fontLibian
        source: "../fonts/FZLBGBK.ttf"
    }

    FontLoader {
        id: fontLi2
        source: "../fonts/FZLE.ttf"
    }
    
    // 背景
    Image {
        id: backgroundImage
        anchors.fill: parent
        source: "../image/anim/officialRankProgressBg.png"
        fillMode: Image.PreserveAspectCrop
        
        // 半透明覆盖层确保内容可读性
        Rectangle {
            anchors.fill: parent
            color: "#f8f5e6"
            opacity: 0.2
        }
    }
    
    // 帅点显示和官阶树
    Item {
        id: pointsAndrankTreePage
        anchors {
            top: titleBar.bottom
            left: parent.left
            right: parent.right
            bottom: parent.bottom
        }
        
        Flickable {
            id: mainFlickable
            anchors.fill: parent
            contentWidth: parent.width
            contentHeight: pointsPanel.height + rankTreeContainer.height + 40
            clip: true
            
            Column {
                width: parent.width
                spacing: 20
                
                // 树状官阶结构容器
                Item {
                    id: rankTreeContainer
                    anchors.horizontalCenter: parent.horizontalCenter
                    width: parent.width
                    height: Math.max(rankTree.height, 420) // 确保最小高度
                    
                    // 树状官阶结构
                    Item {
                        id: rankTree
                        width: parent.width
                        height: 370 // 设置足够的高度以容纳所有节点
                        anchors.centerIn: parent
                        
                        // 定义节点位置
                        property var nodePositions: {
                            "骁卒": Qt.point(150, 350),
                            "校尉": Qt.point(150, 250),
                            "郎将": Qt.point(150, 150),
                            "偏将军": Qt.point(150, 50),

                            "国护军": Qt.point(400, 350),
                            "上将军": Qt.point(400, 200),
                            "将军": Qt.point(400, 50),

                            "国都护": Qt.point(650, 350),
                            "卫将军": Qt.point(650, 150),
                            "车骑将军": Qt.point(750, 150),
                            "骠骑将军": Qt.point(850, 150),

                            "大将军": Qt.point(1050, 150)
                        }
                        
                        // 绘制连接线
                        Canvas {
                            id: connectionCanvas
                            anchors.fill: parent
                            
                            onPaint: {
                                var ctx = getContext("2d");
                                ctx.clearRect(0, 0, width, height);
                                ctx.strokeStyle = "#3e3434";
                                ctx.lineWidth = 13;
                                
                                // 绘制连接线
                                ctx.beginPath();
                                
                                // 骁卒 → 校尉 → 郎将 → 偏将军
                                drawPath(ctx, ["骁卒", "校尉", "郎将", "偏将军"]);

                                // 偏将军 → 将军
                                drawPath(ctx, ["偏将军", "将军"]);
                                
                                // 将军 → 上将军 → 国护军
                                drawPath(ctx, ["将军", "上将军", "国护军"]);
                                
                                // 国护军 → 国都护
                                drawPath(ctx, ["国护军", "国都护"]);

                                // 国都护 → 卫将军
                                drawPath(ctx, ["国都护", "卫将军"]);
                                
                                // 卫将军 → 车骑将军 → 骠骑将军
                                drawPath(ctx, ["卫将军", "车骑将军", "骠骑将军"]);
                                
                                // 骠骑将军 → 大将军
                                drawPath(ctx, ["骠骑将军", "大将军"]);
                                
                                ctx.stroke();
                            }
                            
                            function drawPath(ctx, nodes) {
                                if (nodes.length < 2) return;
                                
                                for (var i = 0; i < nodes.length - 1; i++) {
                                    var from = rankTree.nodePositions[nodes[i]];
                                    var to = rankTree.nodePositions[nodes[i+1]];
                                    
                                    if (from && to) {
                                        if (i === 0) {
                                            ctx.moveTo(from.x, from.y);
                                        }
                                        ctx.lineTo(to.x, to.y);
                                    }
                                }
                            }
                        }

                        // 绘制进度线
                        Canvas {
                            id: progressCanvas
                            anchors.fill: parent
                            
                            onPaint: {
                                var ctx = getContext("2d");
                                ctx.clearRect(0, 0, width, height);
                                ctx.strokeStyle = "#4CAF50";
                                ctx.lineWidth = 11;
                                
                                // 绘制进度线
                                ctx.beginPath();
                                
                                drawPath(ctx, ["骁卒", "校尉"]);
                                if (count >= 4000) drawPath(ctx, ["校尉", "郎将"]);
                                if (count >= 8000) drawPath(ctx, ["郎将", "偏将军"]);
                                if (count >= 12000) drawPath(ctx, ["偏将军", "将军"]);
                                if (count >= 52000) drawPath(ctx, ["将军", "上将军"]);
                                if (count >= 60000) drawPath(ctx, ["上将军", "国护军"]);
                                if (count >= 80000) drawPath(ctx, ["国护军", "国都护"]);
                                if (count >= 130000) drawPath(ctx, ["国都护", "卫将军"]);
                                if (count >= 160000) drawPath(ctx, ["卫将军", "车骑将军"]);
                                if (count >= 200000) drawPath(ctx, ["车骑将军", "骠骑将军"]);
                                if (count >= 250000) drawPath(ctx, ["骠骑将军", "大将军"]);
                                
                                ctx.stroke();
                            }
                            
                            function drawPath(ctx, nodes) {
                                if (nodes.length < 2) return;
                                
                                for (var i = 0; i < nodes.length - 1; i++) {
                                    var from = rankTree.nodePositions[nodes[i]];
                                    var to = rankTree.nodePositions[nodes[i+1]];
                                    
                                    if (from && to) {
                                        ctx.moveTo(from.x, from.y);
                                        if (count >= rankAndCount[nodes[i+1]]) {
                                            ctx.lineTo(to.x, to.y);
                                        }
                                        else{
                                            ctx.lineTo(from.x + ((count - minCount) / (maxCount - minCount)) * (to.x - from.x), from.y + ((count - minCount) / (maxCount - minCount)) * (to.y - from.y));
                                        }
                                    }
                                }
                            }
                        }

                        // 三军统帅分组框
                        Image {
                            id: commanderGroup
                            x: 570 // 起始x坐标
                            y: 90  // 起始y坐标
                            width: 350 // 宽度
                            height: 140 // 高度
                            source: "../image/anim/commanderGroup.png"
                        }

                        Text {
                            x: commanderGroup.x + 15
                            y: commanderGroup.y + commanderGroup.height / 2 - height / 2
                            text: "三\n军\n统\n帅"
                            font.pixelSize: 16
                            font.family: fontSimkai.name
                            font.bold: true
                            color: "#ffe8c2"
                            opacity: 1
                            horizontalAlignment: Text.AlignHCenter
                            verticalAlignment: Text.AlignVCenter
                        }
                        
                        // 官阶节点
                        Repeater {
                            model: [
                                { id: "骁卒", title: "骁卒", general: "", achieved: 0, image: "../image/mark/@!glory_days_xiaozu.png" },
                                { id: "校尉", title: "校尉", general: "", achieved: 4000, image: "../image/mark/@!glory_days_xiaowei.png" },
                                { id: "郎将", title: "郎将", general: "", achieved: 8000, image: "../image/mark/@!glory_days_langjiang.png" },
                                { id: "偏将军", title: "偏将军", general: "", achieved: 12000, image: "../image/mark/@!glory_days_pianjiangjun.png" },
                                { id: "将军", title: "将军", general: "专属武将", achieved: 52000, image: "../image/mark/@!glory_days_jiangjun.png" },
                                { id: "上将军", title: "上将军", general: "专属武将", achieved: 60000, image: "../image/mark/@!glory_days_shangjiangjun.png" },
                                { id: "国护军", title: "国护军", general: "专属武将", achieved: 80000, image: "../image/mark/@!glory_days_guohujun.png" },
                                { id: "国都护", title: "国都护", general: "专属武将", achieved: 130000, image: "../image/mark/@!glory_days_guoduhu.png" },
                                { id: "卫将军", title: "卫将军", general: "专属称号", achieved: 160000, image: "../image/mark/@!glory_days_weijiangjun.png" },
                                { id: "车骑将军", title: "车骑将军", general: "专属称号", achieved: 200000, image: "../image/mark/@!glory_days_cheqijiangjun.png" },
                                { id: "骠骑将军", title: "骠骑将军", general: "专属称号", achieved: 250000, image: "../image/mark/@!glory_days_biaoqijiangjun.png" },
                                { id: "大将军", title: "大将军", general: "专属特效", achieved: 350000, image: "../image/mark/@!glory_days_dajiangjun.png" }
                            ]
                            
                            delegate: Item {
                                x: rankTree.nodePositions[modelData.id].x - width/2
                                y: rankTree.nodePositions[modelData.id].y - height/2
                                width: modelData.title == "大将军" ? 176 : 114
                                height: modelData.title == "大将军" ? 170 : 110
                                
                                // 官阶节点 - 使用图片
                                Item {
                                    anchors.fill: parent

                                    // 旋转光效
                                    Image {
                                        id: lightEffect
                                        source: "../image/anim/light.png"
                                        width: parent.height * 2
                                        height: parent.height * 2
                                        anchors.fill: parent
                                        opacity: 1
                                        visible: minCount == modelData.achieved
                                        
                                        RotationAnimation on rotation {
                                            from: 0
                                            to: 360
                                            duration: 2000
                                            loops: Animation.Infinite
                                            running: true
                                        }
                                    }

                                    Image {
                                        id: selected
                                        source: "../image/anim/officialRank.png"
                                        width: parent.height * 1
                                        height: parent.height * 1
                                        anchors.fill: parent
                                        opacity: 1
                                        visible: minCount == modelData.achieved
                                    }

                                    // 未达成时的灰色图片
                                    Image {
                                        id: grayImage
                                        anchors.fill: parent
                                        source: modelData.image
                                        visible: count < modelData.achieved
                                    }

                                    Colorize {
                                        anchors.fill: grayImage
                                        source: grayImage
                                        saturation: 0
                                        opacity: count < modelData.achieved ? 1 : 0
                                        Behavior on opacity { NumberAnimation { duration: 300 } }
                                    }
                                    
                                    // 已达成时的正常图片
                                    Image {
                                        id: normalImage
                                        anchors.fill: parent
                                        source: modelData.image
                                        visible: count >= modelData.achieved
                                    }
                                    
                                    // 信息提示
                                    Item {
                                        id: infoPanel
                                        anchors {
                                            top: parent.bottom
                                            horizontalCenter: parent.horizontalCenter
                                            topMargin: modelData.title == "大将军" ? 5 : -30
                                        }
                                        width: Math.max(titleText.width, generalText.width) + 26
                                        height: titleText.height + (generalText.visible ? generalText.height + 4 : 0) + 8
                                        
                                        Column {
                                            anchors.centerIn: parent
                                            spacing: 2
                                            
                                            Item {
                                                id: titleItem
                                                width: titleBackground.width
                                                height: titleBackground.height
                                                anchors.horizontalCenter: parent.horizontalCenter
                                                
                                                Image {
                                                    id: titleBackground
                                                    source: "../image/anim/officialRankTitle" + (modelData.title == "大将军" ? "1":"2") + ".png"
                                                    height: titleText.height + 4
                                                    fillMode: Image.PreserveAspectFit
                                                }
                                                
                                                Text {
                                                    id: titleText
                                                    anchors.centerIn: parent
                                                    text: modelData.title
                                                    font.pixelSize: modelData.title == "大将军" ? 18 : 15
                                                    font.family: fontSimkai.name
                                                    font.bold: true
                                                    color: count >= modelData.achieved ? "#ffe6be" : "#ffe8c2"
                                                }
                                            }
                                            
                                            Item {
                                                id: generalItem
                                                width: generalBackground.width
                                                height: generalBackground.height
                                                anchors.horizontalCenter: parent.horizontalCenter
                                                visible: modelData.general !== ""
                                                
                                                Image {
                                                    id: generalBackground
                                                    source: "../image/anim/officialRankGeneral" + (modelData.title == "大将军" ? "1":"2") + ".png"
                                                    height: generalText.height + 2
                                                    fillMode: Image.PreserveAspectFit
                                                }
                                                
                                                Text {
                                                    id: generalText
                                                    anchors.centerIn: parent
                                                    text: modelData.general
                                                    font.pixelSize: modelData.title == "大将军" ? 12 : 11
                                                    font.family: fontSimkai.name
                                                    color: "#fffdee"
                                                }
                                            }
                                        }
                                    }
                                }
                                
                                // 鼠标悬停提示
                                MouseArea {
                                    anchors.fill: parent
                                    hoverEnabled: bottomName === "官阶"
                                    
                                    ToolTip {
                                        id: rankTooltip
                                        text: modelData.title + (count >= modelData.achieved ? "(已晋升)" : "") + "\n" +
                                            "所需帅点: " + modelData.achieved +
                                            (modelData.general ? "\n晋升奖励: " + modelData.general : "")
                                        delay: 500
                                    }
                                    
                                    onEntered: {
                                        rankTooltip.visible = true
                                    }
                                    onExited: {
                                        rankTooltip.visible = false
                                    }
                                }
                            }
                        }
                    }
                }

                // 帅点显示
                Item {
                    id: pointsPanel
                    width: parent.width - 40
                    height: 80
                    anchors.horizontalCenter: parent.horizontalCenter

                    /* Rectangle {
                        anchors.fill: parent
                        color: "white"
                        radius: 8
                        opacity: 0.5
                    } */
                    
                    Column {
                        anchors.centerIn: parent
                        spacing: 5
                        
                        Text {
                            anchors.horizontalCenter: parent.horizontalCenter
                            text: "帅点: " + count + "/" + maxCount
                            font.pixelSize: 18
                            font.family: fontSimkai.name
                            color: "#5d4037"
                            font.bold: true
                        }
                        
                        // 进度条
                        Rectangle {
                            id: jindutiao
                            width: 600
                            height: 14
                            radius: 10
                            color: "#3e3434"
                            
                            Rectangle {
                                width: Math.min(1, (count / maxCount)) * parent.width
                                height: parent.height
                                radius: 10
                                color: "#ffe299" // 金色
                                
                                Text {
                                    anchors.centerIn: parent
                                    text: Math.floor((count / maxCount) * 100) + "%"
                                    font.pixelSize: 12
                                    font.family: fontSimkai.name
                                    color: "#5d4037"
                                    font.bold: true
                                }
                            }

                            Item {
                                anchors {
                                    left: jindutiao.right
                                    leftMargin: 25
                                    verticalCenter: parent.verticalCenter
                                }
                                height: 40

                                Image {
                                    id: jinshengImage
                                    anchors {
                                        left: parent.left
                                        verticalCenter: parent.verticalCenter
                                    }
                                    source: "../image/anim/button.png"
                                    height: parent.height
                                    fillMode: Image.PreserveAspectFit

                                    MouseArea {
                                        anchors.fill: parent
                                        onClicked: {
                                            bottomName = "晋升"
                                        }
                                    }
                                }

                                Text {
                                    id: jinshengText
                                    anchors.centerIn: jinshengImage
                                    text: "提升帅点"
                                    font.pixelSize: 20
                                    font.family: fontSimkai.name
                                    font.bold: true
                                    color: "#5d4037"
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    // 晋升攻略
    Item {
        id: promotionPage
        anchors.centerIn: parent
        anchors.topMargin: -100
        width: 650
        height: 325
        visible: bottomName === "晋升"

        Image {
            id: tishengshuaidianBg
            anchors {
                left: parent.left
                verticalCenter: parent.verticalCenter
            }
            source: "../image/anim/shuaidianBg.png"
            height: parent.height
            fillMode: Image.PreserveAspectFit
        }

        Image {
            id: tishengshuaidianChaImage
            anchors {
                top: tishengshuaidianBg.top
                left: tishengshuaidianBg.right
                topMargin: 10
            }
            source: "../image/anim/cha.png"
            height: 25
            fillMode: Image.PreserveAspectFit

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    bottomName = "官阶"
                }
            }
        }
        
        Flickable {
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.topMargin: 25
            anchors.leftMargin: 110
            width: 520
            height: 280
            contentWidth: 520
            contentHeight: promotionContent.height + 40
            clip: true
            
            Column {
                id: promotionContent
                width: parent.width - 40
                spacing: 20
                
                Text {
                    width: parent.width
                    text: "帅点获取攻略"
                    font.pixelSize: 22
                    font.family: fontSimkai.name
                    font.bold: true
                    color: "#5d4037"
                    horizontalAlignment: Text.AlignHCenter
                }
                
                // 对局任务
                Item {
                    width: parent.width
                    height: playTasks.height + 20

                    Rectangle {
                        anchors.fill: parent
                        color: "white"
                        radius: 8
                        opacity: 0.5
                    }
                    
                    Column {
                        id: playTasks
                        width: parent.width - 20
                        anchors.centerIn: parent
                        spacing: 10
                        
                        Text {
                            text: "对局任务"
                            font.pixelSize: 18
                            font.family: fontLibian.name
                            font.bold: true
                            color: "#5d4037"
                        }
                        
                        Repeater {
                            model: [
                                "• 完成1局斗地主游戏：+200 帅点",
                                "• 完成1局2v2游戏：+400 帅点",
                                "• 完成1局身份游戏：+600 帅点（五人及以上）",
                                "• 上述对局中若包含人机，获取帅点减半",
                                "• 上述对局若不足2分钟，则不获取帅点",
                            ]
                            
                            delegate: Text {
                                text: modelData
                                font.pixelSize: 14
                                font.family: fontLi2.name
                                color: "#333"
                            }
                        }
                    }
                }

                // 胜利任务
                Item {
                    width: parent.width
                    height: winTasks.height + 20

                    Rectangle {
                        anchors.fill: parent
                        color: "white"
                        radius: 8
                        opacity: 0.5
                    }
                    
                    Column {
                        id: winTasks
                        width: parent.width - 20
                        anchors.centerIn: parent
                        spacing: 10
                        
                        Text {
                            text: "胜局奖励"
                            font.pixelSize: 18
                            font.family: fontLibian.name
                            font.bold: true
                            color: "#5d4037"
                        }
                        
                        Repeater {
                            model: [
                                "• 获得1局斗地主游戏胜利：+50 帅点",
                                "• 获得1局2v2游戏胜利：+100 帅点",
                                "• 获得1局身份游戏胜利：+200 帅点（五人及以上）",
                                "• 上述对局中若包含人机，获取帅点减半",
                                "• 上述对局若不足2分钟，则不获取帅点",
                            ]
                            
                            delegate: Text {
                                text: modelData
                                font.pixelSize: 14
                                font.family: fontLi2.name
                                color: "#333"
                            }
                        }
                    }
                }
                
                // 活动奖励
                Item {
                    width: parent.width
                    height: events.height + 20
                    
                    Rectangle {
                        anchors.fill: parent
                        color: "white"
                        radius: 8
                        opacity: 0.5
                    }
                    
                    Column {
                        id: events
                        width: parent.width - 20
                        anchors.centerIn: parent
                        spacing: 10
                        
                        Text {
                            text: "活动奖励"
                            font.pixelSize: 18
                            font.family: fontLibian.name
                            font.bold: true
                            color: "#5d4037"
                        }
                        
                        Repeater {
                            model: [
                                "• 登录奖励：每日登录进行游戏可获得额外帅点",
                                "• 节日活动：节日期间完成特定任务可获得大量帅点",
                            ]
                            
                            delegate: Text {
                                text: modelData
                                font.pixelSize: 14
                                font.family: fontLi2.name
                                color: "#333"
                            }
                        }
                    }
                }
                
                // 快速提升技巧
                Item {
                    width: parent.width
                    height: tips.height + 20
                    
                    Rectangle {
                        anchors.fill: parent
                        color: "white"
                        radius: 8
                        opacity: 0.5
                    }
                    
                    Column {
                        id: tips
                        width: parent.width - 20
                        anchors.centerIn: parent
                        spacing: 10
                        
                        Text {
                            text: "快速提升技巧"
                            font.pixelSize: 18
                            font.family: fontLibian.name
                            font.bold: true
                            color: "#5d4037"
                        }
                        
                        Repeater {
                            model: [
                                "• 组队游戏可以提高胜率，获得更多帅点",
                                "• 关注游戏内活动，参与限时活动获取额外帅点",
                                "• 提升游戏水平，高胜率可以更快积累帅点",
                                "• 合理规划游戏时间，避免疲劳游戏影响效率"
                            ]
                            
                            delegate: Text {
                                text: modelData
                                font.pixelSize: 14
                                font.family: fontLi2.name
                                color: "#333"
                            }
                        }
                    }
                }
            }
        }
    }

    // 帮助
    Item {
        id: helpPage
        anchors {
            top: titleBar.bottom
            left: parent.left
            right: parent.right
            bottom: parent.bottom
            topMargin: 20
        }
        visible: bottomName === "帮助"
        
        Flickable {
            anchors.fill: parent
            contentWidth: parent.width
            contentHeight: helpContent.height + 40
            clip: true
            
            Column {
                id: helpContent
                width: parent.width
                spacing: 20
                
                Text {
                    width: parent.width
                    text: "帮助"
                    font.pixelSize: 22
                    font.family: fontSimkai.name
                    font.bold: true
                    color: "#5d4037"
                    horizontalAlignment: Text.AlignHCenter
                }

                Text {
                    width: parent.width
                    text: "页面正在施工，敬请期待..."
                    font.pixelSize: 14
                    font.family: fontLibian.name
                    color: "#333"
                    horizontalAlignment: Text.AlignHCenter
                }
            }
        }
    }

    // 标题栏
    Item {
        id: titleBar
        width: parent.width
        height: titleBarText.height + 10

        Image {
            id: cornerImage
            anchors {
                top: parent.top
                left: parent.left
            }
            source: "../image/anim/corner.png"
            height: parent.height
            fillMode: Image.PreserveAspectFit
        }

        Image {
            id: returnImage
            anchors {
                left: parent.left
                leftMargin: 20
                verticalCenter: parent.verticalCenter
            }
            source: "../image/anim/return.png"
            height: 20
            fillMode: Image.PreserveAspectFit
        }
        
        Text {
            id: titleBarText
            anchors {
                left: returnImage.right
                leftMargin: 10
                verticalCenter: parent.verticalCenter
            }
            text: " 官  阶"
            font.pixelSize: 20
            font.family: fontSimkai.name
            font.bold: true
            color: "#ffee00"
        }

        MouseArea {
            anchors.fill: parent
            onClicked: {
                root.visible = false;
                if (isLobby) {
                    App.quitPage();
                }
            }
        }
    }
    
    function readGlobalSaveState(sender, sendData) {
        App.setBusy(false);
        // console.log("----------");
        // console.log(sendData);
        // console.log("----------");
        // console.log("typeof sendData == ");
        // console.log(typeof sendData);
        // console.log("----------");
        if (typeof sendData == "string") {
            sendData = JSON.parse(sendData)
        }
        // console.log("typeof sendData == ");
        // console.log(typeof sendData);
        // console.log("----------");
        // console.log(sendData["glory_days_shuaidian"]);
        // console.log(sendData.glory_days_shuaidian);
        if (sendData.hasOwnProperty("glory_days_shuaidian")) {
            // console.log(sendData["glory_days_shuaidian"]);
            count = sendData["glory_days_shuaidian"] * 10;
        }
        updateData();
        // App.showToast(Lua.tr("Update GlobalSaveState done."));
    }

    function updateData() {

        maxCount = 4000;
        minCount = 0;
        if (count >= 350000) {
            maxCount = 350000;
            minCount = 350000;
        }
        else {
            if (count >= 4000) {
                maxCount = 8000;
                minCount = 4000;
            }
            for (var i = 1; i <= 10; i++) {
                if (count >= counts[i]) {
                    maxCount = counts[i-1];
                    minCount = counts[i];
                    break;
                }
            }
        }
        bottomName = "官阶";
        root.visible = true;
        connectionCanvas.requestPaint();

    }
    
    // 初始化完成后绘制连接线
    function loadData(data) {

        if (data.count) {
            isLobby = false;
            // console.log("Not isLobby");
            count = data.count;
            updateData();
        }
        else {
            isLobby = true;
            // console.log("isLobby");
            count = 0;
            ClientInstance.notifyServer("ReadGlobalSaveState","glory_days");
        }

    }

    Component.onCompleted: {
        addCallback("ReadGlobalSaveState", readGlobalSaveState);

        loadData(data);
    }
}